package com.jzdjt.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jzdjt.entity.PaperEntity;
import com.jzdjt.tool.DBUtil;
 
public class UpdateSubjectPaperAnswerServlet extends HttpServlet {

	public UpdateSubjectPaperAnswerServlet() {
		super();
	}

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("UTF-8");

		// 根据标示名获取JSP文件中表单所包含的参数
		String stuid = request.getParameter("stuid");
		String answer = request.getParameter("answer");
		String paperid = request.getParameter("paperid");
		String grade = request.getParameter("grade");
		String subject = request.getParameter("subject");
		boolean result = updateSql(stuid, paperid, grade, subject, answer);

		PrintWriter out = response.getWriter();// 回应请求

		out.write(result + "");
		out.flush();
		out.close();
		System.out.println(result);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//doGet(request, response);
		// 设置客户端的解码方式为utf-8
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("UTF-8");

		// 根据标示名获取JSP文件中表单所包含的参数
		String stuid = request.getParameter("stuid");
		String answer = request.getParameter("answer");
		String paperid = request.getParameter("paperid");
		String grade = request.getParameter("grade");
		String subject = request.getParameter("subject");
		boolean result = updateSql(stuid, paperid, grade, subject, answer);

		PrintWriter out = response.getWriter();// 回应请求

		out.write(result + "");
		out.flush();
		out.close();
		System.out.println(result);
	}

	private boolean updateSql(String stuid, String paperid, String grade, String subject, String answer) {
		boolean rets = false;

		try {
			Date d = new Date(); // 获取当前系统的时间
			// 格式化日期
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			String answerTime = sdf.format(d); // 转为字符串

			String selectsql = "select * from ObjAnswer where paperID='"+paperid+"' and studentID='"+stuid+"'";
            DBUtil dbUtil = new DBUtil(); 
            List<String> getlist = dbUtil.select(selectsql) ;
            if("".equals(answer)){
            	String selectsqlans = "select * from PaperInfo where paperID='"+paperid+"'";
                 List<PaperEntity> getAnslist = dbUtil.QueryFromPaper(selectsqlans) ;
                 if(getAnslist!=null&&getAnslist.size()>0){
					 PaperEntity p = getAnslist.get(0);
					 if(!"".equals(p.getAnswer())){
						 int count = Integer.parseInt(p.getAnswer().toString()) ;
						 if(count>0){
							 for(int i=0;i<count;i++){
								 answer += "#*";
							 }
							 answer = answer.substring(0,answer.length()-1);
						 }
					 }
                 }
            }
			String insertSubjectAnsSql = "insert into ObjAnswer(paperID,studentID,answer,answerTime) values('" + paperid
					+ "','" + stuid + "','" + answer + "','" + answerTime + "')";
            if(getlist!=null&&getlist.size()>0){
            	insertSubjectAnsSql = "update objAnswer set answer = '"+answer+"',answerTime='"+answerTime+"' where paperID='"+paperid+"' and studentID='"+stuid+"'";
            }
			String[] ansarry = answer.split("\\*");  
			List<String> erroinfoSqls = new ArrayList<>();
			
			for (int i = 0; i < ansarry.length; i++) {
				String[] anStrings = new String[ansarry[i].length()];
				if(ansarry[i].length()>1){
					/*for(int j=0;j<ansarry[i].length();j++){ 
						 String astr = "insert into PaperWrongRecord(StudentID,QuestionNum,AnswerTime,PaperGrade,PaperSubject,PaperID) values('" + stuid + "','第" + (i + 1) + "题&" + ansarry[i].toString().substring(j, j+1) + "','"
								+ answerTime + "','" + grade + "','" + subject + "','" + paperid + "')" + ";";
						 erroinfoSqls.add(astr);
					} */
					erroinfoSqls.add("insert into PaperWrongRecord(StudentID,QuestionNum,AnswerTime,PaperGrade,PaperSubject,PaperID) values('" + stuid + "','第" + (i + 1) + "题"   + "','"
							+ answerTime + "','" + grade + "','" + subject + "','" + paperid + "')" + ";");
				}
				else{
					//若答案不等于F，则插入；
					if(!ansarry[i].equals("F"))
						if(ansarry[i].equals("#")){
							erroinfoSqls.add("insert into PaperWrongRecord(StudentID,QuestionNum,AnswerTime,PaperGrade,PaperSubject,PaperID) values('" + stuid + "','第" + (i + 1) + "题','"
									+ answerTime + "','" + grade + "','" + subject + "','" + paperid + "')" + ";");
							/*erroinfoSqls.add("insert into PaperWrongRecord(StudentID,QuestionNum,AnswerTime,PaperGrade,PaperSubject,PaperID) values('" + stuid + "','第" + (i + 1) + "题&A','"
									+ answerTime + "','" + grade + "','" + subject + "','" + paperid + "')" + ";");
							erroinfoSqls.add("insert into PaperWrongRecord(StudentID,QuestionNum,AnswerTime,PaperGrade,PaperSubject,PaperID) values('" + stuid + "','第" + (i + 1) + "题&B','"
									+ answerTime + "','" + grade + "','" + subject + "','" + paperid + "')" + ";");
							erroinfoSqls.add("insert into PaperWrongRecord(StudentID,QuestionNum,AnswerTime,PaperGrade,PaperSubject,PaperID) values('" + stuid + "','第" + (i + 1) + "题&C','"
									+ answerTime + "','" + grade + "','" + subject + "','" + paperid + "')" + ";");
							erroinfoSqls.add("insert into PaperWrongRecord(StudentID,QuestionNum,AnswerTime,PaperGrade,PaperSubject,PaperID) values('" + stuid + "','第" + (i + 1) + "题&D','"
									+ answerTime + "','" + grade + "','" + subject + "','" + paperid + "')" + ";");
							erroinfoSqls.add("insert into PaperWrongRecord(StudentID,QuestionNum,AnswerTime,PaperGrade,PaperSubject,PaperID) values('" + stuid + "','第" + (i + 1) + "题&E','"
									+ answerTime + "','" + grade + "','" + subject + "','" + paperid + "')" + ";"); */
						}
						else {
							erroinfoSqls.add("insert into PaperWrongRecord(StudentID,QuestionNum,AnswerTime,PaperGrade,PaperSubject,PaperID) values('" + stuid + "','第" + (i + 1) + "题"   + "','"
									+ answerTime + "','" + grade + "','" + subject + "','" + paperid + "')" + ";");
						}
						
				}
				
			}
			rets = dbUtil.excuteSql(insertSubjectAnsSql);
			String deleteSql = "delete from PaperWrongRecord where studentID='"+stuid+"' and PaperID='"+paperid+"'";
			rets = dbUtil.excuteSql(deleteSql); 
			rets = dbUtil.excuteSqlByBatch(erroinfoSqls);
			rets = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
			//e.printStackTrace();
			return false;
		}
		return rets;
	}

	 

}